package com.mg.admin.controller.admin;


import com.mg.admin.bean.admin.OrganizationVO;
import com.mg.admin.bean.admin.Result;
import com.mg.admin.bean.admin.UserVO;
import com.mg.admin.constants.Constants;
import com.mg.admin.controller.BaseController;
import com.mg.admin.page.PageBean;
import com.mg.admin.page.PageParam;
import com.mg.admin.service.admin.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;


@Controller
@RequestMapping("/admin/organization/")
public class OrganizationController extends BaseController {

    @Autowired
    private OrganizationService organizationService;

    @RequestMapping(value= LIST,method = RequestMethod.GET)
    public void list(ModelMap modelMap){
        UserVO userVO = currentUser();
        Long orgId = userVO.getOrgId();
        if (orgId == null) orgId = (long) Constants.ONE;
       List<OrganizationVO> voList = organizationService.findChildByParentId(orgId);
        modelMap.put("list",voList);
        modelMap.put("orgId",orgId);
    }


    @RequestMapping(value= ADD,method = RequestMethod.GET)
    public void add(ModelMap modelMap, Long id){
        modelMap.put("parentId",id);
    }


    @RequestMapping(value= EDIT,method = RequestMethod.GET)
    public void edit(ModelMap modelMap, Long id){
        OrganizationVO result = organizationService.findById(id);
        modelMap.put("bean",result);
    }


    @RequestMapping(value= VIEW,method = RequestMethod.GET)
    public void view(ModelMap modelMap, Long id){
       OrganizationVO result = organizationService.findById(id);
        modelMap.put("bean",result);
    }


    @ResponseBody
    @RequestMapping(value= SAVE,method = RequestMethod.POST)
    public void save(OrganizationVO organizationVO){
        OrganizationVO result = organizationService.findById(Long.parseLong(organizationVO.getParentId()));
        organizationVO.setParentIds(result.getParentIds()+ "," +result.getId());
        if (organizationVO.getId() == null){
            organizationService.insertOne(organizationVO);
        }else {
            organizationService.updateOne(organizationVO);
        }
    }



    @ResponseBody
    @RequestMapping(value= DELETE,method = RequestMethod.GET)
    public void delete(Long id){
        organizationService.delete(id);
    }



    /**
     * 右侧列表
     * @param organizationVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = LIST,method = RequestMethod.POST)
    public Result<PageBean> pageList(OrganizationVO organizationVO){
        PageParam pageParam = new PageParam(organizationVO.getStart(),organizationVO.getLength());
        UserVO userVO = currentUser();
        Long orgId = null;
        if (organizationVO.getId() == null){
            orgId = userVO.getOrgId();
        }else {
            orgId = organizationVO.getId();
        }
        PageBean pageBean = organizationService.findByParentId(orgId,pageParam);
        return new Result<>(pageBean);
    }


}